package org.swfer.UICExtend 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import org.swfer.UIC.Dialog;
	
	/**
	 * 弹出框显示容器
	 * @author swfer
	 */
	public class DialogManager extends Sprite 
	{
		public var maxWidth:uint;
		public var maxHeight:uint;
		
		private var hasDraw:Boolean = false;
		
		public function DialogManager() 
		{
			//this.addEventListener(MouseEvent.CLICK, onClick);
			Dialog.dialogLayer = this;
		}
		
		//private function onClick(e:MouseEvent):void 
		//{
			//if (e.target != this) return;
			//
			//if (numChildren == 0) return;
			//
			//removeChild(getChildAt(numChildren-1));
		//}
		
		/**
		 * 添加弹窗显示对象
		 * @param	child
		 * @return
		 */
		override public function addChild(child:DisplayObject):DisplayObject
		{
			drawBg();
			child.alpha = 0;
			child.x = (this.width - child.width) * 0.5;
			child.y = (this.height - child.height) * 0.5;
			new Tween(child, 150, {alpha:1, ease:Tween.SineOutIn } );
			return super.addChild(child);
		}
		
		/**
		 * 移除弹窗显示对象
		 * @param	child
		 * @return
		 */
		override public function removeChild(child:DisplayObject):DisplayObject
		{
			new Tween(child, 200, { alpha:0,ease:Tween.SineOutIn, onComplete:function():void { autoClear(child);}} );
			return child;
		}
		
		private function autoClear(child:DisplayObject):void
		{
			if(child) super.removeChild(child);
			if (numChildren == 0) this.graphics.clear();
		}
		
		private function drawBg():void
		{
			hasDraw = false;
			if (hasDraw || !stage) return;
			
			var g:Graphics = this.graphics;
			g.beginFill(0, 0.05);
			g.drawRect(0, 0, (maxWidth || stage.stageWidth), (maxHeight ||stage.stageHeight));
			g.endFill();
			
			hasDraw = true;
		}
		
		/** 弹窗层根容器。容纳弹窗显示容器 **/
		public function get dialogLayer():DisplayObjectContainer { return this.parent;}
		public function set dialogLayer(cont:DisplayObjectContainer):void
		{
			cont.addChild(this);
		}
		
	}

}